package com.qyhl.module_activities.utils.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qyhl.module_activities.R;
import com.qyhl.module_activities.R2;
import com.qyhl.module_activities.common.ActivityConstant;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ErrorCode;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

public class InputLayout extends RelativeLayout {
    private Activity activity;
    public static final int STATUS_DEFAULT = 0;
    public static final int STATUS_INPUT = 1;
    public int STATUS = STATUS_DEFAULT;

    @BindView(R2.id.input_edittext)
    EditText input_edittext;
    @BindView(R2.id.input_text)
    TextView input_text;
    @BindView(R2.id.send_text)
    TextView send_text;
    @BindView(R2.id.input_singup)
    Button input_singup;
    @BindView(R2.id.input_vote)
    Button input_vote;
    private InputListener inputListener;
    private boolean isSingup = true;
    private boolean isloginsend = true;
    private boolean isNew = false;

    public void setNew(boolean aNew) {
        isNew = aNew;
    }

    //报名状态码
    private int singUpCode = -1;

    public InputLayout(Context context) {
        this(context, null);
    }

    public InputLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setMinimumHeight(StringUtils.dip2px(context, 46));
        View inflate = LayoutInflater.from(context).inflate(R.layout.act_inputlayout_view, this, true);
        ButterKnife.bind(this, inflate);
    }

    @OnClick({R2.id.input_text, R2.id.input_singup, R2.id.send_text, R2.id.input_vote})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.input_text) {
            if (isloginsend) {
                isloginsend = false;
                CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                    @Override
                    public void login(boolean isLogin) {
                        if (isLogin) {
                            activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                            changeSendStatus();
                        } else {
                            Toasty.normal(activity, "尚未登录或登录已失效！").show();
                            RouterManager.routerLogin(activity, 0);
                        }
                        isloginsend = true;
                    }

                    @Override
                    public void error(String s) {

                    }
                });
            }
        } else if (id == R.id.input_singup) {
            inputListener.singup();
        } else if (id == R.id.input_vote) {
            inputListener.dovote();
        } else if (id == R.id.send_text) {
            inputListener.send(input_edittext.getText().toString());
            closeKey();
            clearText();
        }
    }

    public void changeDefaultStatus() {
        if (STATUS == STATUS_INPUT) {
            input_edittext.setVisibility(GONE);
            send_text.setVisibility(GONE);

            input_text.setVisibility(VISIBLE);
            if (isNew) {
                input_vote.setVisibility(VISIBLE);
            }

            if (isSingup) {
                input_singup.setVisibility(VISIBLE);
            }
            STATUS = STATUS_DEFAULT;
        }
    }

    public void changeSendStatus() {
        if (STATUS == STATUS_DEFAULT) {
            input_text.setVisibility(GONE);
            input_singup.setVisibility(GONE);
            input_vote.setVisibility(GONE);

            input_edittext.setVisibility(VISIBLE);
            send_text.setVisibility(VISIBLE);

            input_edittext.setFocusable(true);
            input_edittext.setFocusableInTouchMode(true);
            input_edittext.requestFocus();
            InputMethodManager imm;
            if (activity != null) {
                imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            } else {
                imm = (InputMethodManager) ContextUtilts.getInstance().getmContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            }
            imm.showSoftInput(input_edittext, InputMethodManager.SHOW_IMPLICIT);

            STATUS = STATUS_INPUT;
        }
    }

    public void closeKey() {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(input_edittext.getWindowToken(), 0);
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setInputListener(InputListener inputListener) {
        this.inputListener = inputListener;
    }

    public void clearText() {
        input_edittext.setText("");
    }

    public int getStatus() {
        return STATUS;
    }

    /**
     * 设置报名入口可见
     */
    public void setSingUpVisibility(boolean isSingup) {
        this.isSingup = isSingup;
        if (isSingup) {
            input_singup.setVisibility(VISIBLE);
        } else {
            input_singup.setVisibility(GONE);
        }
    }

    /**
     * 设置投票入口是否可见
     *
     * @param isVisible 显示
     */
    public void setVoteVisibility(boolean isVisible) {
        if (isVisible) {
            input_vote.setVisibility(VISIBLE);
        } else {
            input_vote.setVisibility(GONE);
        }
    }

    public void setVoteEnable(int status, String msg) {
        switch (status) {
            case 0://已结束
                input_vote.setEnabled(false);
                input_vote.setText("投票结束");
                break;
            case 1://进行中
                input_vote.setEnabled(true);
                input_vote.setText("提交投票");
                break;
            case 2://未开始
                input_vote.setEnabled(true);
                input_vote.setText(msg);
                break;
        }
    }

    /**
     * 设置评论入口可见
     */
    public void setCommentVisibility() {
        input_text.setVisibility(VISIBLE);
    }

    /**
     * 设置报名入口状态
     */
    public void setSingUpEnable(int code, String msg) {
        singUpCode = code;
        switch (singUpCode) {
            case ErrorCode.SGIN_ACTIVITY:
            case ErrorCode.SGIN_ACTIVITY_REVIEW:
                input_singup.setText("已报名");
                input_singup.setEnabled(false);
                break;
            case ActivityConstant.SING_UP_OFF:
            case ErrorCode.JOIN_ACTIVITY_ERROR2:
                input_singup.setText(msg);
                input_singup.setEnabled(true);
                break;
            case ActivityConstant.SING_UP_END:
            case ErrorCode.JOIN_ACTIVITY_ERROR4:
                input_singup.setText("报名已结束");
                input_singup.setEnabled(false);
                break;
            case ErrorCode.PLAYER_FULL:
                input_singup.setText("名额已满");
                input_singup.setEnabled(false);
                break;
            default:
                input_singup.setText("立即报名");
                input_singup.setEnabled(true);
        }
    }

    public void setSingUpEnable(int code) {
        setSingUpEnable(code, "");
    }

    public int getSingUpCode() {
        return singUpCode;
    }

    public interface InputListener {
        void singup();

        void send(String text);

        void dovote();
    }
}
